<?php
namespace app\daohang\event;

use think\Controller;

class Category extends Controller
{
    protected $query = [];
    
    public function _initialize()
    {
        $this->query = $this->request->param();

        parent::_initialize();
    }
    
    public function create()
    {
        
        $this->assign('query', $this->query);
        
        $this->assign('items', model('daohang/Category','loglic')->items($this->query));

		return $this->fetch('daohang@category/create');
    }
    
    public function save()
    {
        $data = input('post.');
        
        config('common.validate_name', 'daohang/Category');
        
        config('common.validate_scene', 'save');
        
        //别名唯一 名称可重复
        config('common.where_slug_unique', ['term_module'=>['eq','daohang'],'term_type'=>['eq','category']]);
        
        $term_id = \daicuo\Term::save($data);
        
		if($term_id < 1){
			$this->error(\daicuo\Term::getError());
		}
        
		$this->success(lang('success'));
    }
    
    public function delete()
    {
        DcTermDeleteIds(input('id/a'));
        
        $this->success(lang('success'));
    }
    
    public function edit()
    {
        //查询数据
        $data = \daicuo\Term::get_id(input('id/d',0), false);
        if( is_null($data) ){
            $this->error(lang('empty'));
        }
        
        //定义变量
        $this->assign('data', $data);
        
        $this->assign('items', model('daohang/Category','loglic')->items($data));
        
        return $this->fetch('daohang@category/edit');
    }
    
    public function update()
    {
        $data = input('post.');
        
        config('common.validate_name', 'daohang/Category');
        
        config('common.validate_scene', 'update');
        
        //别名唯一 名称可重复
        config('common.where_slug_unique', ['term_module'=>['eq','daohang'],'term_type'=>['eq','category']]);
        
        if( !\daicuo\Term::update_id($data['term_id'], $data) ){
            $this->error(\daicuo\Term::getError());
        }
        $this->success(lang('success'));
    }
    
    public function status()
    {
        if( !$ids = input('post.id/a') ){
            $this->error(lang('errorIds'));
        }
        $data = [];
        $data['term_status'] = input('request.value/s', 'hidden');
        foreach($ids as $key=>$id){
            \daicuo\Term::update(['term_id'=>$id], $data, '');
        }
        $this->success(lang('success'));
    }
    
    public function preview()
    {
        $info = \daicuo\Term::get_id(input('id/d',0), false);
        if(!$info){
            $this->error(lang('empty'));
        }
        //去掉后台入口文件
        $url = str_replace($this->request->baseFile(), '', daohangUrlCategory($info));
        //跳转至前台
        $this->redirect($url,302);
    }
    
    public function index()
    {
        if( $this->request->isAjax() ){
            //查询参数
            $args = array();
            $args['cache']    = false;
            $args['result']   = 'level';
            $args['type']     = 'category';
            $args['search']   = $this->query['searchText'];
            $args['limit']    = $this->query['pageSize'] ;
            $args['page']     = $this->query['pageNumber'];
            $args['sort']     = DcEmpty($this->query['sortName'], 'term_id');
            $args['order']    = DcEmpty($this->query['sortOrder'], 'desc');
            $args['status']   = $this->query['term_status'];
            $args['controll'] = $this->query['term_controll'];
            $args['action']   = $this->query['term_action'];
            //自定义字段排序
            if( !in_array($args['sort'],['term_id','term_order','term_parent','term_count']) ){
                $args['meta_key'] = $args['sort'];
                $args['sort']     = 'meta_value_num';
            }
            //搜索关键字只能返回数组
            if($args['search'] || $args['controll'] || $args['action']){
                $args['result']  = 'array';
            }
            //查询数据
            $list = DcTermSelect( DcArrayEmpty($args) );
            if( is_null($list) ){
                return json(['total'=>0,'data'=>'']);
            }
            return json($list);
		}
        //地址栏参数
        $this->assign('query', $this->query);
        //表单筛选字段
        $this->assign('items', model('daohang/Category','loglic')->items($this->query));
        //表格列字段
        $this->assign('columns', model('daohang/Category','loglic')->columns($this->query));
        //加载模板
		return $this->fetch('daohang@category/index');
	}
}